home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2215 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.3 KB  |  116 lines

  1. Path: newsbf02.news.aol.com!not-for-mail
  2. From: jwpirie@aol.com (JWPirie)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C Programming Question
  5. Date: 16 Jan 1996 10:04:26 -0500
  6. Organization: America Online, Inc. (1-800-827-6364)
  7. Sender: root@newsbf02.news.aol.com
  8. Message-ID: <4dgepq$2tt@newsbf02.news.aol.com>
  9. References: <8B8E4D4.009200AFF3.uuout@bbs1984.chi.il.us>
  10. Reply-To: jwpirie@aol.com (JWPirie)
  11. NNTP-Posting-Host: newsbf02.mail.aol.com
  12.  
  13. I searched high and low for a device to format my values with commas.  I
  14. finally gave up and wrote my own code.
  15.  
  16. This is in C++, so if you're just using C you're out of luck.
  17.  
  18. //**********************************************************************
  19. //  The header file, commas.h:
  20. class comm {
  21. protected:
  22.     double x;
  23. public:
  24.     comm();                          // Constructors
  25.     comm(double d);
  26.  
  27.     inline operator double();        // Conversion
  28.     inline operator float();
  29.     inline operator int();
  30.     inline operator long();
  31.  
  32.     // Stream
  33.     friend ostream& operator<<(ostream &s, comm d);
  34. };
  35.  
  36.  
  37. //******************************************************************
  38. // The implementation file, commas.cpp:
  39.  
  40. comm::comm() { x = 0; }
  41. comm::comm(double d) { x = d; }
  42.  
  43. inline comm::operator double() { return double(x); }
  44. inline comm::operator float() { return float(x); }
  45. inline comm::operator int() { return int(x); }
  46. inline comm::operator long() { return long(x); }
  47.  
  48. ostream& operator<<(ostream &s, comm d) {
  49.     double absval = d.x >= 0 ? d.x : -d.x;      // Absolute value of d
  50.     int precis = s.precision();                 // Digits to right of
  51. decimal
  52.     double precismag = pow(10, precis);
  53.     absval = long(absval * precismag) / precismag;
  54.         // Round to req'd precision
  55.     double base = absval;
  56.     int neg = d.x >= 0 ? FALSE : TRUE;          // Negative sign
  57. needed
  58.     int digits =                                // Digits to left of
  59. decimal
  60.         ((base == 0) ? 0 : max(0, floor(log10(base)))) + 1;
  61.     int commas = floor((digits - 1) / 3);       // Number of commas
  62. needed
  63.     int decpt = precis > 0 ? TRUE : FALSE;      // Decimal point
  64. needed
  65.     double order = pow(10, commas * 3);         // 1, 10^3, 10^6,
  66. 10^9, etc.
  67.  
  68.     // Set up formatting based on current field width for the stream.
  69.     // Fill width with whitespace to right justify value.
  70.     long flagsave = s.flags();
  71.     int wid = s.width();
  72.     s.setf(ios::fixed, ios::showpoint);
  73.     s.width(0);
  74.     s.precision(0);
  75.     int needed = neg + digits + commas + decpt + precis;
  76.     for (int i = 1; i <= wid - needed; i++)
  77.         s << " ";
  78.  
  79.     // Now write out the value, with commas
  80.     if (neg)
  81.         s << "-";
  82.     double rem;
  83.     while (order > 1) {
  84.         rem = floor(base / order);
  85.         if (base == absval)
  86.             s << int(rem) << ",";
  87.         else
  88.             s << setw(3) << setfill('0') << int(rem) << ",";
  89.         base -= rem * order;
  90.         order /= 1000;
  91.     }
  92.     if (base == absval)
  93.         s << setprecision(precis) << base;
  94.     else
  95.         s << setw(3 + decpt + precis) << setfill('0') 
  96.             << setprecision(precis) << base;
  97.  
  98.     s.flags(flagsave);
  99.     return s;
  100. }
  101.  
  102. ***********************************************
  103.  
  104. And finally, in implementation just cast your value to type "comm", as
  105. follows:
  106.  
  107. fptr << "  Avg balance:  $ " << comm(total_bal / loan_count) << "\n";
  108.  
  109. Feel free to e-mail me if questions.
  110. --------------------
  111. John Pirie
  112. Cohane Rafferty Securities, Inc.
  113. Harrison, NY
  114. "I'd keep playing -- I don't think the heavy stuff's comin' down for quite
  115. a while yet."
  116.